import request, { METHOD } from '../service/request.service';
import { formatTableResult, defaultPagination, goodsResponse, goodResponse } from './common';



export function getGood(id) {
  uni.showLoading();
  return new Promise((resolve) => {
    resolve(goodResponse)
  }).then(response => {
      uni.hideLoading();
      return response;
    })
    .catch(error => {
      uni.hideLoading();
      return error;
    });
}
export function getGoodList(params) {
  return new Promise((resolve) => {
    resolve(goodsResponse)
  }).then(formatTableResult);
}

export function createGood(data) {
  uni.showLoading();

  return request({
    url: '/good',
    method: METHOD.POST,
    data,
  })
    .then(response => {
      uni.hideLoading();
      uni.showToast({
        title: '创建成功',
        duration: 1000,
      });
      return response;
    })
    .catch(error => {
      uni.hideLoading();
      return error;
    });
}

export function modifyGood(id) {
  return request({
    url: `/good`,
    method: METHOD.PUT,
  });
}

export function getCollectGoodList(params) {
  return request({
    url: `/goods/collect`,
    method: METHOD.GET,
    params: {
      ...defaultPagination,
      ...params,
    },
  }).then(formatTableResult);
}

export function getUserGoodList(params) {
  return request({
    url: `/goods/user`,
    method: METHOD.GET,
    params: {
      ...defaultPagination,
      ...params,
    },
  }).then(formatTableResult);
}

